package com.fly.estore.service.Impl;

import com.fly.estore.dao.*;
import com.fly.estore.domain.*;
import com.fly.estore.framework.util.CommonUtil;
import com.fly.estore.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/4/16.
 */
@Service("ProductService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductBrandDAO productBrandDAO;
    @Autowired
    ProductCatalogDAO productCatalogDAO;
    @Autowired
    ProductSpecDAO productSpecDAO;
    @Autowired
    ProductCatalogSpecDAO productCatalogSpecDAO;
    @Autowired
    ProductDAO productDAO;
    @Autowired
    ProductSKUDAO productSKUDAO;

    //-------------------------begin 商品品牌--------------------------------///

    /**
     * 保存商品品牌
     * @param brand 商品品牌对象
     * @return 影响数据行数
     */
    public int saveProductBrand(ProductBrand brand) {
        int update = 0;
        if (StringUtils.isBlank(brand.getProductBrandId())){
            //brandId为空，新增数据
            brand.setProductBrandId(CommonUtil.genPK());
            update = productBrandDAO.addProductBrand(brand);
        }
        else{
            //更新数据
            update = productBrandDAO.updateProductBrand(brand);
        }
        return update;
    }

    /**
     * 根据id删除商品品牌
     * @param productBrandId    品牌id
     * @return 影响数据行数
     */
    public int deleteBrandById(String productBrandId) {
        return productBrandDAO.deleteById(productBrandId);
    }

    /**
     * 根据id获取商品品牌
     * @param productBrandId 品牌id
     * @return 品牌对象
     */
    public ProductBrand getBrandById(String productBrandId) {
        ProductBrand productBrand = new ProductBrand();
        productBrand = productBrandDAO.getById(productBrandId);
        return productBrand;
    }

    public List<ProductBrand> getBrandList() {
        List<ProductBrand> brandList = new ArrayList<ProductBrand>();
        brandList = productBrandDAO.getList();
        return brandList;
    }

    public int getProductCount() {
        return productDAO.getProductCount();
    }

    //-------------------------end 商品品牌--------------------------------///


    //-------------------------begin 商品分类 ------------------------------///
    public int saveProductCatalog(ProductCatalog catalog) {
        int update = 0;
        if (StringUtils.isBlank(catalog.getProductCatalogId())){
            catalog.setProductCatalogId(CommonUtil.genPK());
            update = productCatalogDAO.addProductCatalog(catalog);

            for(ProductCatalogSpec catalogSpec : catalog.getCatalogSpecList()){
                catalogSpec.setProductCatalogId(catalog.getProductCatalogId());
                productCatalogSpecDAO.addProductCatalogSpec(catalogSpec);
            }
        }
        else{
            update = productCatalogDAO.updateProductCatalog(catalog);
            productCatalogSpecDAO.deleteByCatalogId(catalog.getProductCatalogId());
            for(ProductCatalogSpec catalogSpec : catalog.getCatalogSpecList()){
                catalogSpec.setProductCatalogId(catalog.getProductCatalogId());
                productCatalogSpecDAO.addProductCatalogSpec(catalogSpec);
            }
        }
        return update;
    }

    public int deleteCatalogById(String productCatalogId) {
        int update = 0;
        update = productCatalogDAO.deleteById(productCatalogId);
        return update;
    }

    public ProductCatalog getCatalogById(String productCatalogId) {
        ProductCatalog catalog = new ProductCatalog();
        catalog = productCatalogDAO.getById(productCatalogId);
        return catalog;
    }

    public List<ProductCatalog> getCatalogList() {
        return productCatalogDAO.getList();
    }
    //-----------------------end 商品分类----------------------------//

    //------------------------begin 商品规格-------------------------//

    public int saveProductSpec(ProductSpec spec) {
        int update = 0;
        if (StringUtils.isBlank(spec.getProductSpecId())){
            spec.setProductSpecId(CommonUtil.genPK());
            update = productSpecDAO.addProductSpec(spec);
        }
        else{
            update = productSpecDAO.updateProductSpec(spec);
        }
        return update;
    }

    public int deleteSpecById(String productSpecId) {
        return productSpecDAO.deleteById(productSpecId);
    }

    public ProductSpec getSpecById(String productSpecId) {
        return productSpecDAO.getById(productSpecId);
    }

    public List<ProductSpec> getSpecList() {
        return productSpecDAO.getList();
    }
    //----------------------------end 商品规格------------------------------//

    //----------------------------begin 商品类型规格-----------------------//

    public int deleteCatalogSpecByCatalogId(String productCatalogId) {
        return productCatalogSpecDAO.deleteByCatalogId(productCatalogId);
    }

    public List<ProductCatalogSpec> getCatalogSpecListByCatalogId(String productCatalogId) {
        return productCatalogSpecDAO.getByCatalogId(productCatalogId);
    }

    //-------------------------------end 商品类型规格 -------------------------------//


    //-----------------------------begin 商品---------------------------------------//

    public int saveProduct(ProductInfo productInfo, String productId) {
        int update = 0;
        if (StringUtils.isBlank(productInfo.getProductId())){
            //新增商品
            productInfo.setProductId(productId);
            update = productDAO.addProduct(productInfo);
        }
        else{
            //更新商品
            update = productDAO.updateProduct(productInfo);
        }
        return update;
    }

    public int deleteProductById(String productId) {
        return productDAO.deleteById(productId);
    }

    public ProductInfo getProductById(String productId) {
        return productDAO.getById(productId);
    }

    public List<ProductInfo> getProductList() {
        return productDAO.getList();
    }

    public List<ProductInfo> queryProductList(String productName, String productCode, String catalogId, String brandId) {
        return productDAO.queryList(productName, productCode, catalogId, brandId);
    }

    //-----------------------------end 商品 ----------------------------------------//

    //-----------------------------begin 商品属性 -----------------------------------------//

    public int saveProductSKU(ProductSKU productSKU) {
        int update = 0;
        if (StringUtils.isBlank(productSKU.getProductSkuId())){
            productSKU.setProductSkuId(CommonUtil.genPK());
            update = productSKUDAO.addProductSKU(productSKU);
        }
        else{
            update = productSKUDAO.updateProductSKU(productSKU);
        }
        return update;
    }

    public int deleteProductSKUById(String productSkuId) {
        return productSKUDAO.deleteById(productSkuId);
    }

    public ProductSKU getSKUById(String productSkuId) {
        return productSKUDAO.getById(productSkuId);
    }

    public List<ProductSKU> getSKUListByProductId(String productId) {
        return productSKUDAO.getByProductId(productId);
    }
}
